<template>
  <el-card>
    <!-- 头部 -->
    <template v-slot:header>
      <div class="category">
        <span>销售额类别占比</span>
        <el-radio-group v-model="radio" size="small">
          <el-radio-button label="全部渠道"></el-radio-button>
          <el-radio-button label="线上"></el-radio-button>
          <el-radio-button label="门店"></el-radio-button>
        </el-radio-group>
      </div>
    </template>
    <!-- 饼图 -->
    <div>
      <!-- 容器 -->
      <div class="barCharts" ref="chart"></div>
    </div>
  </el-card>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'Category',
  data () {
    return {
      radio: '全部渠道'
    }
  },
  mounted () {
    let myChart = echarts.init(this.$refs.chart)
    let option = {
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c} ({d}%)'
      },
      legend: {
        top: 'center',
        right: '10%',
        orient: 'vertical'
      },
      series: [
        {
          name: '销售额占比',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: true,
            position: 'outside'
          },
          labelLine: {
            show: true
          },
          data: [
            { value: 1048, name: '搜索引擎' },
            { value: 735, name: '直接访问' },
            { value: 580, name: '邮件营销' },
            { value: 484, name: '联盟广告' },
            { value: 300, name: '视频广告' }
          ]
        }
      ]
    }
    option && myChart.setOption(option)
    myChart.on('mouseover', (params) => {
      myChart.setOption({
        title: {
          text: params.name,
          subtext: params.value,
          left: "center",
          top: "center",
          textStyle: {
            fontSize: 30
          },
          subtextStyle: {
            fontSize: 20
          }
        }
      })
    })
  }
}
</script>

<style scoped>
.category {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.barCharts {
  width: 100%;
  height: 350px;
}
</style>
